---
layout: example
categories: example/v1.0.0
version: v1.0.0
title: Opacity control
description: Click and drag a slider to adjust the opacity of an overlay.
tags:
  - ui
---
<style>
.ui-opacity {
  background:#FFF;
  position:absolute;
  left:10px;
  top:70px;
  height:200px;
  width:28px;
  border:1px solid rgba(0,0,0,0.4);
  border-radius:3px;
  z-index:1000;
  }
.ui-opacity .handle {
  position:absolute;
  background:#404040;
  left:0;
  top:20px;
  width:26px;
  height:10px;
  border-radius:1px;
  cursor:pointer;
  cursor:ns-resize;
  }
  .ui-opacity .handle:hover {
    background:#303030;
    }
</style>

<div id='map'></div>
<div id='control' class='ui-opacity'>
  <div id='handle' class='handle'></div>
</div>

<script>
var handle = document.getElementById('handle'),
    start = false,
    startTop;

var map = L.mapbox.map('map')
    .setView([43.6654, -79.4775], 14)
    .addLayer(L.mapbox.styleLayer('mapbox://styles/mapbox/light-v10'));

var overlay = L.mapbox.tileLayer('aibram.Aerial', {
      zIndex: 2
    })
    .addTo(map);

document.onmousemove = function(e) {
    if (!start) return;
    // Adjust control.
    handle.style.top = Math.max(-5, Math.min(195, startTop + parseInt(e.clientY, 10) - start)) + 'px';
    // Adjust opacity.
    overlay.setOpacity(1 - (handle.offsetTop / 200));
};

handle.onmousedown = function(e) {
    // Record initial positions.
    start = parseInt(e.clientY, 10);
    startTop = handle.offsetTop - 5;
    return false;
};

document.onmouseup = function(e) {
    start = null;
};
</script>
